package demoMod.plugins.mod.sts.module.steps;

import com.intellij.ide.util.projectWizard.ModuleWizardStep;
import com.intellij.openapi.options.ConfigurationException;
import demoMod.plugins.mod.sts.DataSetting;
import demoMod.plugins.mod.sts.module.vo.ProjectConfigVO;
import demoMod.plugins.mod.sts.ui.ProjectConfigUI;

import javax.swing.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class SpireModModuleWizardStep extends ModuleWizardStep {

    private ProjectConfigUI projectConfigUI;

    public SpireModModuleWizardStep(ProjectConfigUI projectConfigUI) {
        this.projectConfigUI = projectConfigUI;
    }

    @Override
    public JComponent getComponent() {
        return projectConfigUI.getComponent();
    }

    @Override
    public void updateDataModel() {

    }

    @Override
    public boolean validate() throws ConfigurationException {
        ProjectConfigVO projectConfig = DataSetting.getInstance().getProjectConfig();
        projectConfig._modId = projectConfigUI.getModIdField().getText();
        projectConfig._modName = projectConfigUI.getModNameField().getText();
        projectConfig._modDescription = projectConfigUI.getDescriptionField().getText();
        projectConfig._steamPath = projectConfigUI.getSteamPathField().getText();
        projectConfig._packageName = projectConfigUI.getPackageField().getText();
        projectConfig._version = projectConfigUI.getVersionField().getText();
        ListModel<String> model = projectConfigUI.getDependencyList().getModel();
        List<String> dep = new ArrayList<>();
        int[] indexes = projectConfigUI.getDependencyList().getSelectedIndices();
        for (int index : indexes) {
            dep.add(model.getElementAt(index));
        }
        projectConfig._modDependencies = dep.toArray(new String[0]);


        indexes = projectConfigUI.getSubscribersList().getSelectedIndices();
        dep.clear();
        model = projectConfigUI.getSubscribersList().getModel();
        for (int index : indexes) {
            dep.add(model.getElementAt(index));
        }
        projectConfig.usedSubscribers = dep;
        projectConfig.addMonsters = projectConfigUI.getChkAddMonsters().isSelected();
        projectConfig.addEvents = projectConfigUI.getChkAddEvents().isSelected();
        projectConfig.addPotions = projectConfigUI.getChkAddPotions().isSelected();

        projectConfig._languages = new ArrayList<>();
        for (Field field : ProjectConfigUI.class.getDeclaredFields()) {
            if (field.getName().startsWith("chk") && !field.getName().startsWith("chkAdd")) {
                field.setAccessible(true);
                try {
                    JCheckBox checkBox = (JCheckBox) field.get(projectConfigUI);
                    if (checkBox.isSelected()) {
                        String lan = field.getName().replace("chk", "");
                        projectConfig._languages.add(lan.toLowerCase(Locale.ROOT));
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return super.validate();
    }
}
